<?php
/**
 * Created by PhpStorm.
 * User: a
 * Date: 2016/6/7
 * Time: 17:32
 */
namespace Admin\Model;
use Think\Model;

class ProfitModel extends Model 
{
	protected $trueTableName = 'bypal_invest_profit';


	/**
	 * 收益管理列表页数据
	 */
	public function get_profit_list($name,$title,$time,$type,$range) 
	{
		$select_sql = "select t1.invest_income_id,t2.cust_name,t4.invest_id,t4.invest_title,t1.start_time,t1.end_time,t1.invest_interest,t1.period_money_count,t1.profit_time,t1.invest_detail_id,t1.customer_id,t1.period_number 
				from bypal_invest_profit t1 
				left join bypal_cust_info t2 on t1.customer_id=t2.cust_id 
				left join bypal_invest_detail t3 on t1.invest_detail_id=t3.invest_detail_id 
				left join bypal_invest_project t4 on t3.invest_id=t4.invest_id 
				where 1=1";

		//	搜索查询条件
		if ($name != '') 
		{
			$select_sql .= " and t2.cust_name like '%$name%'";
		}
		if ($title != '') 
		{
			$select_sql .= " and t4.invest_title like '%$title%'";
		}
		if ($time != '') 
		{
			$select_sql .= " and t1.end_time = '$time'";
		}
		if ($type != '') 
		{
			if ($type == '1') 
			{
				$timeNow = time();
				$select_sql .= " and t1.profit_time is null and UNIX_TIMESTAMP(t1.end_time) >= $timeNow";
			}
			elseif ($type == '2') 
			{
				$select_sql .= " and t1.profit_time is not null";
			}
			elseif ($type == '3') 
			{
				$timeNow = time();
				$select_sql .= " and t1.profit_time is null and UNIX_TIMESTAMP(t1.end_time) < $timeNow";
			}
		}
		if ($range != '') 
		{
			$now = time();
			$compare = $now + $range;
			$select_sql .= " and UNIX_TIMESTAMP(t1.end_time) between $now and $compare";
		}
		$select_sql .= " order by UNIX_TIMESTAMP(t1.end_time) ASC";

		$Model = new \Think\Model();
		$values = $Model->query($select_sql);

		foreach ($values as $key => $value) 
		{
			if ($value['profit_time'] == '') 
			{
				if (strtotime($value['end_time']) >= time()) 
				{
					$values[$key]['profit_time'] = '未到放款时间';
				}
				else
				{
					$values[$key]['profit_time'] = '超时未放款';
				}
			}
			else
			{
				$values[$key]['profit_time'] = '已放款';
			}
		}

		return $values;
	}

}